package com.dkm.ec.activity.domain;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.biz.gl.exception.PartinException;
import com.dkm.biz.gl.exception.StockDeficiencyException;
import com.dkm.ec.activity.persistence.dao.CommonActivityPartinDao;
import com.dkm.ec.activity.persistence.entity.CommonActivityPartinPo;
import com.dkm.ec.activity.persistence.entity.CommonActivitySkuPo;
import com.dkm.ec.activity.repository.CommonActivityPartinRepository;
import com.dkm.ec.activity.repository.CommonActivitySkuRepository;
import com.dkm.ec.stock.domain.Stock;
import com.dkm.ec.stock.persistence.dao.StockQueryDao;
import com.dkm.ec.stock.persistence.entity.StockPo;
import com.dkm.ec.stock.repository.StockRepository;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by fanjinyuan on 2017/8/11.
 */
@SuppressWarnings("serial")
@Service
@Scope("prototype")
public class CommonActivityPartin extends AbstractDomain<String, CommonActivityPartinPo> {

    private CommonActivityPartinDao commonActivityPartinDao;
    @Resource
    private StockQueryDao stockQueryDao;
    @Resource
    private CommonActivitySkuRepository activitySkuRepository;
    @Resource
    private CommonActivityPartinRepository commonActivityPartinRepository;
    @Resource
    private StockRepository stockRepository;


    protected void init(){
        commonActivityPartinDao = SpringHelper.getBean(CommonActivityPartinDao.class);
        setDao(commonActivityPartinDao);
    }

    public void partin(CommonActivityPartinPo po) throws StockDeficiencyException {
        // 判断商家是否已参加该活动
        Integer count = commonActivityPartinRepository.countForShopId(po.getCommonactivityid(), po.getShopid());
        if(count >= 1) {
            throw new PartinException("同一个商家不能参加同一次活动");
        }
        // 合并相同sku
        List<CommonActivitySkuPo> skuPoList = mergeSku(po.getSkuPoList());
        //check stock available
        for(CommonActivitySkuPo sku : skuPoList) {
            StockPo stock = stockQueryDao.get(sku.getStockid());
            int availableStock = stock.getQty() - stock.getQtyReserved();
            if(sku.getPartinnum() > availableStock){
                throw new StockDeficiencyException(String.format("sku: %s 库存不足", stock.getSkuId()));
            }
            // 减少总库存数
            stock.setQty(stock.getQty() - sku.getPartinnum());
            Stock stockIns = stockRepository.newInstance(stock);
            stockIns.update();
        }

        po.setPartintime(new Date());
        this.setData(po);
        this.create();

        //check stock available
        for(CommonActivitySkuPo sku : po.getSkuPoList()) {
            sku.setPartinid(po.getId());
            sku.setAvailableNum(sku.getPartinnum());
            sku.setIsReturnStock("0");
            activitySkuRepository.newInstance(sku).create();
        }

    }

    public List<CommonActivitySkuPo> mergeSku(List<CommonActivitySkuPo> skuPoList) {
        Map<String, CommonActivitySkuPo> map = new HashMap<String, CommonActivitySkuPo>();
        for (CommonActivitySkuPo po : skuPoList) {
            String stockId = po.getStockid();
            if (map.containsKey(stockId)) {
                Integer partiNnum = map.get(stockId).getPartinnum();
                partiNnum += po.getPartinnum();
                map.get(stockId).setPartinnum(partiNnum);
            } else {
                map.put(stockId, po);
            }
        }
        List<CommonActivitySkuPo> list = new ArrayList<CommonActivitySkuPo>();
        Iterator<CommonActivitySkuPo> iterator = map.values().iterator();
        while(iterator.hasNext()) {
            CommonActivitySkuPo next = iterator.next();
            list.add(next);
        }
        return list;
    }


}
